<?php
/**
 * 咨询师-信息索引表
 *
 * User: kaikaic
 * Date: 2018/12/17
 * Time: 19:54
 */

namespace app\psy\service;

use service\BasicService;
use think\Db;

class ConsultantIndexService extends BasicService {

    public function init() {
        $this->dataTable = ['PsyIndexType', 'PsyIndexFactions'];
    }

    /**
     * 擅长类型
     * @param Array $consultant,咨询师信息
     */
    public function updateTypeIndex($consultant) {
        $consultantID = $consultant['id'];
        $typeList = unserialize($consultant['goodat_ids']);

        // 清除旧数据
        $where = array();
        $where['consultant_id'] = $consultantID;
        $this->remove($where);

        for ($i=0; $i<count($typeList); $i++) {
            $params = array();
            $params['consultant_id'] = $consultantID;
            $params['type_id'] = $typeList[$i];

            $this->insert($params);
        }
    }

    /**
     * 咨询流派
     * @param Array $consultant,咨询师信息
     */
    public function updateFactionsIndex($consultant) {
        $consultantID = $consultant['id'];
        $factionsList = unserialize($consultant['factions_ids']);

        // 清除旧数据
        $where = array();
        $where['consultant_id'] = $consultantID;
        $this->remove($where,1);

        for ($i=0; $i<count($factionsList); $i++) {
            $params = array();
            $params['consultant_id'] = $consultantID;
            $params['factions_id'] = $factionsList[$i];

            $this->insert($params, 1);
        }
    }

}